package com.jhws.business.comm.service;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.jhws.business.comm.bo.TblBuildingDimBo;
import com.jhws.business.comm.bo.TblCommDimBo;
import com.jhws.common.Page;
import com.jhws.core.db.BaseDao;
import com.jhws.core.exception.BizException;
import com.jhws.business.comm.dao.ITblBuildingDimDAO;
import com.jhws.util.AccountsUtils;
import com.jhws.util.StringUtils;
import org.apache.commons.collections.MapUtils;


public class TblBuildingDimServiceSpringImpl implements ITblBuildingDimService {
    private ITblBuildingDimDAO tblBuildingDimDAO;

    @Resource
    BaseDao BaseDao;
    private static String[] numberToEnglish = new String[]{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"};
    @Resource
    private ITblCommDimService ICommService;

    public ITblBuildingDimDAO getTblBuildingDimDAO() {
        return tblBuildingDimDAO;
    }

    public void setTblBuildingDimDAO(ITblBuildingDimDAO tblBuildingDimDAO) {
        this.tblBuildingDimDAO = tblBuildingDimDAO;
    }

    public Page findTblBuildingDim(int start, int num, String whereClause) throws Exception {
        Page page = tblBuildingDimDAO.findTblBuildingDim(start, num, whereClause);
        return page;
    }

    public TblBuildingDimBo save(TblBuildingDimBo bo) throws Exception {
        return tblBuildingDimDAO.save(bo);
    }

    public List<TblBuildingDimBo> findTblBuildingDimByWhere(String whereClause) throws Exception {
        return tblBuildingDimDAO.findTblBuildingDimByWhere(whereClause);
    }

    @Override
    public String getBuildingAliasName(String commCode, String areaName, String buildingName) {
        String hqlstr = "from TblBuildingDimBo where comm_code = '" + commCode + "' and area_name = '" + Integer.valueOf(areaName) + "' and building_name = '" + Integer.valueOf(buildingName) + "'";
        List<TblBuildingDimBo> list = BaseDao.findList(hqlstr);
        if (list != null && list.size() != 0) {
            return StringUtils.isEmpty(list.get(0).getAreaAliasName()) ?
                    areaName.length() < 2 ? "0" + areaName + "栋" : areaName + "栋" : list.get(0).getAreaAliasName();
        }
        return areaName.length() < 2 ? "0" + areaName + "栋" : areaName + "栋";
    }

    @Override
    public String getUnitAliasName(String commCode, String areaName, String buildingName) {
        String hqlstr = "from TblBuildingDimBo where comm_code = '" + commCode + "' and area_name = '" + Integer.valueOf(areaName) + "' and building_name = '" + Integer.valueOf(buildingName) + "'";
        List<TblBuildingDimBo> list = BaseDao.findList(hqlstr);
        if (list != null && list.size() != 0) {
            return StringUtils.isEmpty(list.get(0).getBuildingAliasName()) ?
                    AccountsUtils.appendZeroBuilding(buildingName) + "单元" : list.get(0).getBuildingAliasName();
        }
        return AccountsUtils.appendZeroBuilding(buildingName) + "单元";
    }

    public List<Map<String, Object>> getUnitsInfoForExport(String commCode){
        String sql = "select area_name as areaName," +
                     "area_alias_name as areaAliasName," +
                     "building_name as unitName," +
                     "unit_number as floorNum," +
                     "room_number as roomNum " +
                    "from tbl_building_dim where comm_code = '" + commCode + "'";
        return BaseDao.findListBySql(sql);
    }

    @Override
    public List findTblBuildingDimBySelectWhere(String select, String whereClause) throws Exception {
        return tblBuildingDimDAO.findTblBuildingDimBySelectWhere(select, whereClause);
    }

    @Override
    public List<String> findAllOutdoorOrWallMachineByComm(String commCode) throws Exception {
        // TODO Auto-generated method stub
        List<String> owList = new ArrayList<String>();
        String where = "where 1=1 and comm_code = '" + commCode + "'";
        List<TblBuildingDimBo> bos = findTblBuildingDimByWhere(where);
        if (bos.size() == 0)
            throw new BizException("-1", "getDevNumberForManager", "未查找到单元数据");
        for (int i = 0; i < bos.size(); i++) {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(commCode);
            if (bos.get(i).getAreaName().length() == 1) {
                stringBuilder.append("0").append(bos.get(i).getAreaName());
            } else {
                stringBuilder.append(bos.get(i).getAreaName());
            }

            if (bos.get(i).getBuildingName().length() == 1) {
                stringBuilder.append("0").append(bos.get(i).getBuildingName());
            } else {
                stringBuilder.append(bos.get(i).getBuildingName());
            }

            for (int devNum = 0; devNum < bos.get(i).getDevNumber(); devNum++) {
                String sipNum = stringBuilder.toString() + "000" + String.valueOf(devNum + 1);
                owList.add(sipNum);
            }
        }

        where = "where 1=1 and comm_code = '" + commCode + "'";
        List<TblCommDimBo> comms = ICommService.findTblCommDimByWhere(where);
        for (int i = 0; i < comms.get(0).getGateDevNum(); i++) {
            String sipNum = commCode + "0000000" + numberToEnglish[i];
            owList.add(sipNum);
        }
        return owList;
    }

    public TblBuildingDimBo findTblBuildingDimById(String BuildingCode) {
        return tblBuildingDimDAO.getById(BuildingCode);
    }

    @Override
    public int getCountByComm(String comm) {
        return tblBuildingDimDAO.getBuildingCountByComm(comm);
    }

    public void removeDim(String BuildingCode) {
        tblBuildingDimDAO.deleteTblBuildingDim(BuildingCode);
    }

    public void removeBuildingByComm(String commCode) {
        tblBuildingDimDAO.deleteTblBuildingDimByComm(commCode);
    }

    public int getUnitDevNumber(String Room) throws Exception {
        if(Room.length() == 8)
            Room += "0000";
        String comm = AccountsUtils.getCommCode(Room);
        String area = AccountsUtils.getBuilding(Room);
        int building = Integer.valueOf(AccountsUtils.getUnit(Room));
        String where = "where 1=1 and comm_code = '" + comm + "' and area_name = '" + Integer.valueOf(area) + "' and building_name = '"
                + building + "'";

        List<TblBuildingDimBo> bos = findTblBuildingDimByWhere(where);
        if (bos.size() == 0)
            return -1;

        return bos.get(0).getDevNumber();
    }

    public BigInteger getMaxBuildingNum(String commCode) {
        return tblBuildingDimDAO.getMaxBuildingNum(commCode);
    }

    public Map<String, Object> findBuildingListPage(Map param) {
        String whereClause = "1=1 ";

        String commCode = MapUtils.getString(param, "commCode", "");
        if(StringUtils.isNotEmpty(commCode)){
            whereClause += " and commCode = '"+commCode+"'";
        }

        String areaName = MapUtils.getString(param, "areaName", "");
        if (StringUtils.isNotEmpty(areaName)) {
            whereClause += " and areaName = '" + areaName + "'";
        }

        String buildingName = MapUtils.getString(param, "buildingName", "");
        if (StringUtils.isNotEmpty(buildingName)) {
            whereClause += " and buildingName = '"+ buildingName + "'";
        }

        String hql = "from TblBuildingDimBo where " + whereClause;
        hql += " order by cast(areaName as int) asc";

        return BaseDao.queryPage(hql);

    }
}
